<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/*
 * FIXME: Description goes here .. 
 * 
 * @package RedCross_Form
 * @category RedCross
 */

class RedCross_Form_Element_DateTime extends Zend_Form_Element_Xhtml
{
	protected $_dateFormat = '%year%-%month%-%day% %hours%:%minutes%'; //2010-03-03 14:21
	protected $_minutes;
	protected $_hours;
	protected $_day;
	protected $_month;
	protected $_year;
	
	public function __construct($spec, $options = null)
	{
		$this->addPrefixPath(
		'RedCross_Form_Decorator',
		'RedCross/Form/Decorator/',
		'decorator'
		);
		
		parent::__construct($spec, $options);
	}
	
	public function loadDefaultDecorators()
	{
		if ($this->loadDefaultDecoratorsIsDisabled()) 
		{
			return;
			
		}
		
		
		$decorators = $this->getDecorators();
		if (empty($decorators))
		{
			$this	->addDecorator('Datetime')
					->addDecorator('Errors')
					->addDecorator('Description', array(
						'tag'	=> 'p',
						'class' => 'description'
					));
		}
	}
	
	public function setMinutes($value)
	{
		$this->_minutes = (int) $value;
		return $this;
	}
	
	public function getMinutes()
	{
		if($this->_minutes >= 10){
			return $this->_minutes;
		} else {
			return '0' . $this->_minutes;
		}
		
	}
	
	public function setHours($value)
	{
		$this->_hours = (int) $value;
		return $this;
	}
	
	public function getHours()
	{
		if($this->_hours >= 10){
			return $this->_hours;
		} else {
			return '0' . $this->_hours;
		}
		return $this->_hours;
	}
	
	public function setDay($value)
	{
		$this->_day = (int) $value;
		return $this;
	}
	
	public function getDay()
	{
		if($this->_day >= 10){
			return $this->_day;
		} else {
			return '0' . $this->_day;
		}
	}
	
	public function setMonth($value)
	{
		$this->_month = (int) $value;
		return $this;
	}
	
	public function getMonth()
	{
		if($this->_month >= 10){
			return $this->_month;
		} else {
			return '0' . $this->_month;
		}
		
	}
	
	public function setYear($value)
	{
		$this->_year = (int) $value;
		return $this;
	}
	
	public function getYear()
	{
		return $this->_year;
	}
	
	public function setValue($value)
	{
		
		if (is_int($value))
		{
			$this	->setMinutes(date('M', $value))
					->setHours(date('H', $value))
					->setDay(date('d', $value))
					->setMonth(date('m', $value))
					->setYear(date('Y', $value));
		} 
		elseif (is_string($value))
		{
			$this 	->setMinutes(substr($value,14,0))
					->setHours(substr($value,11,2))
					->setDay(substr($value,8,2))
					->setMonth(substr($value,5,2))
					->setYear(substr($value,0,4));
		}
		elseif (is_array($value) 
					&& (isset($value['minutes'])) 
						&& (isset($value['hours'])) 
							&& (isset($value['day'])) 
								&& (isset($value['month'])) 
									&& (isset ($value['year'])))
		{
			
			$this	->setMinutes(ereg_replace('[^0-9]', '', $value['minutes']))
					->setHours(ereg_replace('[^0-9]', '', $value['hours']))
					->setDay($value['day'])
					->setMonth($value['month'])
					->setYear($value['year']);
		} 
		else 
		{
			throw new Exception('Invalid date value provided');
		}
		
		return $this;
	}
	
	public function getValue()
	{
			// I don't use strtotime here becuse it tries to guess what date you meant, which causes problems with validation.
			$time = $this->getYear() . '-' . $this->getMonth() . '-' . $this->getDay() . ' ' . $this->getHours() . ':' . $this->getMinutes() . ':00';
			return $time;
	}
	
}